package redundancy_abnormal_txn

import (
	"git.multiverse.io/eventkit/common/model/mgmt"
	"git.multiverse.io/eventkit/dxc-server/models"
	jsoniter "github.com/json-iterator/go"
)

var (
	json = jsoniter.ConfigCompatibleWithStandardLibrary
)

func PageQueryRedundancyAbnormalTransactions(nodeId string, page int, pageSize int) ([]*mgmt.PageQueryRedundancyAbnormalTxnRecordResult, error) {
	//req := mgmt.PageQueryRedundancyAbnormalTxnRecordRequest{
	//	NodeId:   nodeId,
	//	Page:     page,
	//	PageSize: pageSize,
	//}
	//reqBody, _ := json.Marshal(req)
	//request := mesh.NewMeshRequest(reqBody)
	//request.WithOptions(
	//	mesh.WithTopicTypeOps(),
	//	mesh.WithEventID(config.Cfg.Management.PageQueryRedundancyAbnormalTxnRecordTopicID),
	//	mesh.WithMaxRetryTimes(1),
	//	mesh.WithTimeout(time.Duration(config.Cfg.Management.PageQueryRedundancyAbnormalTxnRecordTimeoutMilliseconds)*time.Millisecond),
	//	mesh.WithCodec(codec.BuildCustomCodec(&codec_text.Encoder{}, &codec_text.Decoder{})),
	//)
	//meshClient := client.DefaultClient
	//var respBody []byte
	//if _, err := meshClient.SyncCall(context.Background(), request, &respBody); err != nil {
	//	return nil, err
	//}
	//
	//commRes := &mgmt.CommResponse{}
	//if err := json.Unmarshal(respBody, commRes); nil != err {
	//	return nil, err
	//}
	//if 0 != commRes.Code {
	//	return nil, errors.Errorf("")
	//}
	//results := make([]*mgmt.PageQueryRedundancyAbnormalTxnRecordResult, 0)
	//return results, nil
	return nil, nil
}

func ReportRedundancyAbnormalTransactionToDtsMgmt(origin *models.RootTransaction, rootXid string, methodName string, methodFields ...interface{}) {
	//if !config.Cfg.Management.Enable {
	//	log.Warns("Report transaction to DTS management has disabled, skip...")
	//	return
	//}
	//fieldJsons := make([]string, 0)
	//for _, field := range methodFields {
	//	if by, err := json.Marshal(field); err != nil {
	//		fieldJsons = append(fieldJsons, string(by))
	//	}
	//}
	//redundancyAbnormalTxnRequest := mgmt.DoRedundancyAbnormalTxnRecordRequest{
	//	NodeId:           config.Cfg.NodeId,
	//	RootXId:          rootXid,
	//	MethodName:       methodName,
	//	MethodFieldsJson: strings.Join(fieldJsons, constant.REDUNDANCY_ABNORMAL_TRANSACTION_METHOD_FIELDS_JSON_SPLIT),
	//	CreateTime:       time.Now(),
	//}
	//if origin != nil {
	//	by, _ := json.Marshal(origin)
	//	redundancyAbnormalTxnRequest.GlobalTxnJson = string(by)
	//}
	//reqBody, _ := json.Marshal(redundancyAbnormalTxnRequest)
	//
	//request := mesh.NewMeshRequest(reqBody)
	//request.WithOptions(
	//	mesh.WithTopicTypeOps(),
	//	mesh.WithEventID(config.Cfg.Management.RedundancyAbnormalTransactionRecordReportTopicID),
	//	mesh.WithMaxRetryTimes(1),
	//	mesh.WithTimeout(time.Duration(config.Cfg.Management.RedundancyAbnormalTransactionRecordReportTimeoutMilliseconds)*time.Millisecond),
	//	mesh.WithCodec(codec.BuildCustomCodec(&codec_text.Encoder{}, &codec_text.Decoder{})),
	//)
	//meshClient := client.DefaultClient
	//var respBody []byte
	//if _, err := meshClient.SyncCall(context.Background(), request, &respBody); err != nil {
	//	log.Errors("")
	//}
	//commRes := &mgmt.CommResponse{}
	//if err := json.Unmarshal(respBody, commRes); nil != err {
	//	log.Errors("")
	//}
	//
	//if 0 != commRes.Code {
	//	log.Errors("")
	//}
	//log.Infosf("Successfully report transaction[%++v] to DTS management!", origin)
}
